import { register, login, logout, getInfo } from '@/request/api/login'
import { getUserToken, setUserToken, removeUserToken } from '@/utils/cookies'
import { info } from 'autoprefixer'

/**
 * 模块：用户user
 */
const user = {
  state: {
    token: getUserToken(), // 登录token
    userId: null,
    name: '',
    avatar: '',
    roles: null,
    permissions: []
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_USER_ID: (state, id) => {
      state.userId = id;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    }
  },
  actions: {
    // 注册
    Register({ commit }, data) {
      return new Promise((resolve, reject) => {
        register(data).then(res => {
          setUserToken(res.detail.token);
          commit('SET_TOKEN', res.detail.token);
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        login(data).then(res => {
          setUserToken(res.detail.token);
          commit('SET_TOKEN', res.detail.token);
          resolve();
        }).catch(error => {
          reject(error);
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        // 无token时
        if(!state.token) {
          resolve();
          return ;
        }
        // 请求用户信息
        getInfo().then(res => {
          if(res.detail){
            // 用户信息设定
            const user = res.detail;
            if (user.role && user.role.length > 0) { // 验证返回的roles是否是一个非空数组
              commit('SET_ROLES', user.role);
            } else {
              commit('SET_ROLES', []);
            }
            commit('SET_USER_ID', user.id);
            commit('SET_NAME', user.nickName);
            commit('SET_AVATAR', user.headPhoto);
          }else{
            // 用户信息取得失败，或者token失效
            removeUserToken();
            commit('SET_USER_ID', null);
          }
          resolve();
        }).catch(error => {
          // 用户信息取得失败，或者token失效
          removeUserToken();
          commit('SET_USER_ID', null);
          reject(error)
        })
      })
    },
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '');
          commit('SET_ROLES', []);
          commit('SET_PERMISSIONS', []);
          removeUserToken();
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        removeUserToken();
        resolve();
      })
    }
  }
}
export default user
